package com.wanli.storemobile.homepage;

import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import anet.channel.util.StringUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.gson.Gson;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.wanli.storemobile.base.BaseFragment;

import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wanli.storemobile.R;
import com.wanli.storemobile.base.DataCallBack;
import com.wanli.storemobile.bean.BaseResponseBean;
import com.wanli.storemobile.bean.ImageBean;
import com.wanli.storemobile.bean.StoreDataBasic;
import com.wanli.storemobile.bean.StoreDataBean;
import com.wanli.storemobile.bean.StoreDataCompany;
import com.wanli.storemobile.bean.StoreDataInfo;
import com.wanli.storemobile.bean.StoreDeatilsBean;
import com.wanli.storemobile.bean.UserInfoBean;
import com.wanli.storemobile.event.ALiDiscernEvent;
import com.wanli.storemobile.event.SignBitmapEvent;
import com.wanli.storemobile.event.StoreDataEvent;
import com.wanli.storemobile.homepage.model.HomePageModelImpl;
import com.wanli.storemobile.homepage.model.IHomePageModel;
import com.wanli.storemobile.login.ForgetPwdActivity;
import com.wanli.storemobile.login.LoginActivity;
import com.wanli.storemobile.utils.Base64BitmapUtil;
import com.wanli.storemobile.utils.DateTimeUtil;
import com.wanli.storemobile.utils.Dp2Px;
import com.wanli.storemobile.utils.GlideEngine;
import com.wanli.storemobile.utils.GlideUtils;
import com.wanli.storemobile.utils.NumberFormatUtils;
import com.wanli.storemobile.utils.PermissionsManager;
import com.wanli.storemobile.utils.SPManager;
import com.wanli.storemobile.utils.ToastUtil;
import com.wanli.storemobile.widget.ViewLoading;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static android.app.Activity.RESULT_OK;

/**
 * 上次资料
 */
public class StoreUpDataFragment extends BaseFragment {

    public static final String TITLE_TYPE = "type";
    public static final String IS_SHOW = "isShow";
    public static final String MERCHANT_ID = "merchant_id";
    public static final String QR_CODE_DATA = "qr_code_data";
    public static final String CHANNEL_TYPE = "channel_type";
    public static final String QUICK_INCOME = "quick_income";

    @BindView(R.id.ic_prcture_1)
    ImageView icPrcture1;
    @BindView(R.id.ic_prcture_2)
    ImageView icPrcture2;
    @BindView(R.id.ic_prcture_3)
    ImageView icPrcture3;
    @BindView(R.id.ic_prcture_4)
    ImageView icPrcture4;
    @BindView(R.id.ic_prcture_5)
    ImageView icPrcture5;
    @BindView(R.id.ic_prcture_6)
    ImageView icPrcture6;
    @BindView(R.id.ic_prcture_7)
    ImageView icPrcture7;
    @BindView(R.id.ic_prcture_8)
    ImageView icPrcture8;
    @BindView(R.id.ic_prcture_9)
    ImageView icPrcture9;
    @BindView(R.id.ic_prcture_10)
    ImageView icPrcture10;
    @BindView(R.id.ic_prcture_20)
    ImageView icPrcture20;
    @BindView(R.id.ic_prcture_21)
    ImageView icPrcture21;
    @BindView(R.id.ic_prcture_22)
    ImageView icPrcture22;
    @BindView(R.id.ll_non_leg)
    LinearLayout llNonLeg;
    @BindView(R.id.ll_business)
    LinearLayout llBusiness;
    @BindView(R.id.ll_protocol)
    LinearLayout llProtocol;
    @BindView(R.id.ll_hand_photo)
    LinearLayout llHandPhoto;
    @BindView(R.id.tv_sign)
    TextView tvSign;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.ll_bank_account_permit_pic)
    LinearLayout ll_bank_account_permit_pic;
    @BindView(R.id.v_bank_account_permit_pic)
    View v_bank_account_permit_pic;
    @BindView(R.id.iv_acquiring_agreement)
    ImageView iv_acquiring_agreement;

    @BindView(R.id.iv_1)
    ImageView iv_1;
    @BindView(R.id.iv_2)
    ImageView iv_2;
    @BindView(R.id.iv_3)
    ImageView iv_3;
    @BindView(R.id.iv_4)
    ImageView iv_4;
    @BindView(R.id.iv_5)
    ImageView iv_5;
    @BindView(R.id.iv_6)
    ImageView iv_6;
    @BindView(R.id.iv_7)
    ImageView iv_7;
    @BindView(R.id.iv_8)
    ImageView iv_8;
    @BindView(R.id.iv_9)
    ImageView iv_9;
    @BindView(R.id.iv_10)
    ImageView iv_10;
    @BindView(R.id.iv_11)
    ImageView iv_11;
    @BindView(R.id.tv_download_power_attorney_template)
    TextView tv_download_power_attorney_template;



    private String type;
    private static final int REQUEST_CODE_SIGN = 10001;


    private String prctureDemo10 = "https://wnb-dc.oss-cn-qingdao.aliyuncs.com/app_img/agent/kdb_pic_1.png";
    private String prctureDemo11 = "https://wnb-dc.oss-cn-qingdao.aliyuncs.com/app_img/agent/kdb_pic_2.png";
    private String prctureDemo12 = "https://wnb-dc.oss-cn-qingdao.aliyuncs.com/app_img/agent/kdb_pic_3.png";

    private BottomSheetDialog selectPicDialog;
    private PermissionsManager permissionsManager;
    private String[] permissions;
    private int openType;//0 相册，1相机
    private List<LocalMedia> mediaList = new ArrayList<>();
    //1 2 3 4 5 6 7 8  20  21 22
    private int imgTpye = 1;
    private View viewProtocol1;
    //    private View viewProtocol2;
    private View viewProtocol3;
    private String currentTime;
    private StoreDataBasic storeDataBasic;
    private StoreDataCompany storeDataCompany;
    private StoreDataInfo storeDataInfo;
    private StoreDataBean storeDataBean;
    private Gson mGson = new Gson();
    private IHomePageModel homePageModel = new HomePageModelImpl();
    private String imgPath1, imgPath2, imgPath3, imgPath4, imgPath5, imgPath6, imgPath7, imgPath8, imgPath9, imgPath10, imgPath20, imgPath21, imgPath22;

    private Bitmap bitmapProtocol1;
    //    private Bitmap bitmapProtocol2;
    private String bitmapProtocolUrl2;
    private Bitmap bitmapProtocol3;
    private StoreDeatilsBean.DataBean storeBean;
    private String merchant_id;
    private String qr_code_data;
    private int quick_income;

    private String fileBase64Str1 = "";
    private String fileBase64Str3 = "";
    private String isShow;
    private String channel_type;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_store_up_data, container, false);
        ButterKnife.bind(this, view);
        registerEventBus();
        Bundle bundle = getArguments();
        if (bundle != null) {
            type = bundle.getString(TITLE_TYPE);
            isShow = bundle.getString(IS_SHOW);
            StoreDataEvent dataEvent = EventBus.getDefault().getStickyEvent(StoreDataEvent.class);
            if (dataEvent != null) {
                storeBean = dataEvent.getDataBean();
            }
            merchant_id = bundle.getString(MERCHANT_ID);
            qr_code_data = bundle.getString(QR_CODE_DATA);
            channel_type = bundle.getString(CHANNEL_TYPE);
            quick_income = bundle.getInt(QUICK_INCOME);
        }
        initView();
        initData();
        initEvent();

        return view;
    }

    private void initEvent() {
        iv_1.setOnClickListener(view -> {
            Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
            intent.putExtra("type","1");
            startActivity(intent);
        });
        iv_2.setOnClickListener(view -> {
            Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
            intent.putExtra("type","2");
            startActivity(intent);
        });
        iv_3.setOnClickListener(view -> {
            Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
            intent.putExtra("type","3");
            startActivity(intent);
        });
        iv_4.setOnClickListener(view -> {
            Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
            intent.putExtra("type","4");
            startActivity(intent);
        });
        iv_5.setOnClickListener(view -> {
            Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
            intent.putExtra("type","5");
            startActivity(intent);
        });
        iv_6.setOnClickListener(view -> {
            Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
            intent.putExtra("type","6");
            startActivity(intent);
        });
        iv_7.setOnClickListener(view -> {
            Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
            intent.putExtra("type","7");
            startActivity(intent);
        });
        iv_8.setOnClickListener(view -> {
            Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
            intent.putExtra("type","8");
            startActivity(intent);
        });
        iv_9.setOnClickListener(view -> {
            Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
            intent.putExtra("type","9");
            startActivity(intent);
        });
        iv_10.setOnClickListener(view -> {
            Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
            intent.putExtra("type","10");
            startActivity(intent);
        });
        iv_11.setOnClickListener(view -> {
            Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
            intent.putExtra("type","12");
            startActivity(intent);
        });
        tv_download_power_attorney_template.setOnClickListener(view -> {
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.iv_ffsqwts);
            saveScreenShot(bmp);
        });
    }

    private void initView() {
        UserInfoBean userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);

        if (null!=userInfoBean){
            UserInfoBean.DataBean data = userInfoBean.getData();
            channel_type =String.valueOf(data.getChannel_type());
        }
        storeDataBean = new StoreDataBean();
        llProtocol.setVisibility(View.VISIBLE);
        if ("1".equals(type)) {

            //无需营业执照
//            llBusiness.setVisibility(View.GONE);
            //快速入网
            if (quick_income == 1) {
                llHandPhoto.setVisibility(View.GONE);
                llProtocol.setVisibility(View.GONE);
            }
//            ll_bank_account_permit_pic.setVisibility(View.GONE);
//            v_bank_account_permit_pic.setVisibility(View.GONE);
        } else if ("2".equals(type)) {
//            llBusiness.setVisibility(View.VISIBLE);
//            ll_bank_account_permit_pic.setVisibility(View.VISIBLE);
//            v_bank_account_permit_pic.setVisibility(View.VISIBLE);
        } else if ("3".equals(type)) {
            //无需电子签名  和 收单协议
//            llBusiness.setVisibility(View.VISIBLE);
            llProtocol.setVisibility(View.GONE);
//            ll_bank_account_permit_pic.setVisibility(View.VISIBLE);
//            v_bank_account_permit_pic.setVisibility(View.VISIBLE);
        }

        if ("0".equals(isShow)){
            tvCommit.setVisibility(View.GONE);
        }
    }

    public void initData() {
        permissionsManager = new PermissionsManager(mActivity);
        currentTime = DateTimeUtil.format(new Date());

        GlideUtils.loadImage(mActivity, iv_acquiring_agreement, prctureDemo10);

        if (storeBean != null) {
            imgPath1 = storeBean.getDoor_pic();
            imgPath2 = storeBean.getInside_pic();
            imgPath3 = storeBean.getCashier_desk_pic();
            imgPath4 = storeBean.getBank_card_front_pic();
            imgPath5 = storeBean.getLegal_id_card_front_pic();
            imgPath6 = storeBean.getLegal_id_card_back_pic();
            imgPath7 = storeBean.getLegal_id_card_hand_pic();
            imgPath8 = storeBean.getLicense_pic();
            imgPath9 = storeBean.getBank_card_back_pic();
            imgPath10 = storeBean.getBank_account_permit_pic();
            imgPath20 = storeBean.getNon_leg_settle_auth_pic();
            imgPath21 = storeBean.getNon_leg_idcard_front_pic();
            imgPath22 = storeBean.getNon_leg_idcard_back_pic();

            GlideUtils.loadImage(mActivity, icPrcture1, imgPath1);
            GlideUtils.loadImage(mActivity, icPrcture2, imgPath2);
            GlideUtils.loadImage(mActivity, icPrcture3, imgPath3);
            GlideUtils.loadImage(mActivity, icPrcture4, imgPath4);
            GlideUtils.loadImage(mActivity, icPrcture5, imgPath5);
            GlideUtils.loadImage(mActivity, icPrcture6, imgPath6);
            GlideUtils.loadImage(mActivity, icPrcture7, imgPath7);
            GlideUtils.loadImage(mActivity, icPrcture8, imgPath8);
            GlideUtils.loadImage(mActivity, icPrcture9, imgPath9);
            GlideUtils.loadImage(mActivity, icPrcture10, imgPath10);
            GlideUtils.loadImage(mActivity, icPrcture20, imgPath20);
            GlideUtils.loadImage(mActivity, icPrcture21, imgPath21);
            GlideUtils.loadImage(mActivity, icPrcture22, imgPath22);
        }
    }


    @Subscribe
    public void onSignBitmapEvent(SignBitmapEvent event) {
        /**
         * 收单协议2
         */
        bitmapProtocolUrl2 = event.getBitmapProtocolurl();
        storeDataBean.setCollect_pic_two(bitmapProtocolUrl2);
    }

    @Subscribe
    public void onALiDiscernEvent(ALiDiscernEvent event) {
        //身份证 正面
        if (1 == event.getType()) {
            imgPath5 = event.getImgPath();
            GlideUtils.loadImage(mActivity, icPrcture5, imgPath5);
        }
        if (2 == event.getType()) {
            imgPath6 = event.getImgPath();
            GlideUtils.loadImage(mActivity, icPrcture6, imgPath6);
        }
        //营业执照
        if (3 == event.getType()) {
            imgPath8 = event.getImgPath();
            GlideUtils.loadImage(mActivity, icPrcture8, imgPath8);
        }
        //银行卡号
        if (4 == event.getType()) {
            imgPath4 = event.getImgPath();
            GlideUtils.loadImage(mActivity, icPrcture4, imgPath4);
        }
        //银行卡反面
        if (9 == event.getType()) {
            imgPath9 = event.getImgPath();
            GlideUtils.loadImage(mActivity, icPrcture9, imgPath9);
        }
        //开户许可证
        if (10 == event.getType()) {
            imgPath10 = event.getImgPath();
            GlideUtils.loadImage(mActivity, icPrcture10, imgPath10);
        }
        //非法人正面
        if (11 == event.getType()) {
            imgPath21 = event.getImgPath();
            GlideUtils.loadImage(mActivity, icPrcture21, imgPath21);
        }
        //非法人反面面
        if (12 == event.getType()) {
            imgPath22 = event.getImgPath();
            GlideUtils.loadImage(mActivity, icPrcture22, imgPath22);
        }
    }

    /**
     * 收单协议1
     */
    public void saveProtocol1() {
        int width = (int) Dp2Px.dp2Px(600); // 屏幕宽度（像素）
        int height = (int) Dp2Px.dp2Px(847); // 屏幕高度（像素）

        viewProtocol1 = LayoutInflater.from(mActivity).inflate(R.layout.view_protocol_1, null, false);
        TextView merchant_name = viewProtocol1.findViewById(R.id.tv_merchant_name);
        TextView merchant_address = viewProtocol1.findViewById(R.id.tv_merchant_address);
        if ("1".equals(type)) {
            merchant_name.setText("商户_" + storeDataBasic.getLegal_name());
        } else if ("2".equals(type)) {
            merchant_name.setText(storeDataCompany.getLicense_full_name());
        }
        merchant_address.setText(storeDataBasic.getProvince() + storeDataBasic.getCity() + storeDataBasic.getArea() + storeDataBasic.getAddress());
        layoutView(viewProtocol1, width, height);
        bitmapProtocol1 = loadBitmapFromView(viewProtocol1);

        fileBase64Str1 = Base64BitmapUtil.bitmapToBase64(bitmapProtocol1, 20);
    }

    /**
     * 收单协议3
     */
    public void saveProtocol3() {
        int width = (int) Dp2Px.dp2Px(600); // 屏幕宽度（像素）
        int height = (int) Dp2Px.dp2Px(847); // 屏幕高度（像素）

        viewProtocol3 = LayoutInflater.from(mActivity).inflate(R.layout.view_protocol_3, null, false);
        TextView date_time1 = viewProtocol3.findViewById(R.id.tv_date_time1);
        TextView date_time2 = viewProtocol3.findViewById(R.id.tv_date_time2);
        TextView date_time3 = viewProtocol3.findViewById(R.id.tv_date_time3);
        TextView store_name = viewProtocol3.findViewById(R.id.tv_store_name);
        TextView permit_number = viewProtocol3.findViewById(R.id.tv_permit_number);
        TextView legal_person = viewProtocol3.findViewById(R.id.tv_legal_person);
        TextView legal_idNumber = viewProtocol3.findViewById(R.id.tv_legal_idNumber);
        TextView bank_name = viewProtocol3.findViewById(R.id.tv_bank_name);
        TextView bank_number = viewProtocol3.findViewById(R.id.tv_bank_number);
        TextView account_type1 = viewProtocol3.findViewById(R.id.tv_account_type1);
        TextView account_type2 = viewProtocol3.findViewById(R.id.tv_account_type2);
        TextView account_name = viewProtocol3.findViewById(R.id.tv_account_name);
        TextView tv_rate_3 = viewProtocol3.findViewById(R.id.tv_rate_3);
        TextView tv_rate_4 = viewProtocol3.findViewById(R.id.tv_rate_4);
        TextView tv_rate_5 = viewProtocol3.findViewById(R.id.tv_rate_5);
        TextView tv_t1 = viewProtocol3.findViewById(R.id.tv_t1);
        TextView tv_D0 = viewProtocol3.findViewById(R.id.tv_D0);
        TextView tv_D1 = viewProtocol3.findViewById(R.id.tv_D1);
        TextView store_address = viewProtocol3.findViewById(R.id.tv_store_address);


        String[] times = currentTime.split("-");
        date_time1.setText(times[0]);
        date_time2.setText(times[1]);
        date_time3.setText(times[2]);

        if ("1".equals(type)) {
            store_name.setText("商户_" + storeDataBasic.getLegal_name());
        } else if ("2".equals(type)) {
            store_name.setText(storeDataCompany.getLicense_full_name());
        }
        if ("1".equals(type)) {
            permit_number.setText("");
        } else if ("2".equals(type)) {
            permit_number.setText(storeDataCompany.getLicense_no());
        }
        legal_person.setText(storeDataBasic.getLegal_name());
        legal_idNumber.setText(storeDataBasic.getLegal_id_card_no());
        bank_name.setText(storeDataInfo.getBranch());
        bank_number.setText(storeDataInfo.getBank_card_no());
        //0-个人账户, 1-公司账户
        if ("0".equals(storeDataInfo.getAccount_type())) {
            account_type1.setVisibility(View.GONE);
            account_type2.setVisibility(View.VISIBLE);
        } else {
            account_type1.setVisibility(View.VISIBLE);
            account_type2.setVisibility(View.GONE);
        }
        account_name.setText(storeDataInfo.getHolder());

        //是否是D0  有DO 手续费0.05  优惠手续费 费率(0.38) - 0.05
        // T1   无 DO 手续费   优惠手续费 费率(0.38)
        //到账时间 0  D0  ,1  T1
        if ("0".equals(storeDataInfo.getSettlement_cycle())) {
            String rate = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(storeDataInfo.getRate()) / 100);
            rate = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(rate) - Double.parseDouble("0.05"));
            tv_rate_3.setText(rate);
            tv_rate_4.setText(rate);
            tv_rate_5.setText("0.05");
            tv_D0.setVisibility(View.VISIBLE);
            tv_D1.setVisibility(View.GONE);
            tv_t1.setVisibility(View.GONE);
        } else if ("1".equals(storeDataInfo.getSettlement_cycle())) {
            String rate = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(storeDataInfo.getRate()) / 100);
            tv_rate_3.setText(rate);
            tv_rate_4.setText(rate);
            tv_rate_5.setText("");
            tv_D0.setVisibility(View.GONE);
            tv_D1.setVisibility(View.GONE);
            tv_t1.setVisibility(View.VISIBLE);
        } else {
            String rate = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(storeDataInfo.getRate()) / 100);
            tv_rate_3.setText(rate);
            tv_rate_4.setText(rate);
            tv_rate_5.setText("");
            tv_D0.setVisibility(View.GONE);
            tv_D1.setVisibility(View.VISIBLE);
            tv_t1.setVisibility(View.GONE);
        }

        store_address.setText(storeDataBasic.getProvince() + storeDataBasic.getCity() + storeDataBasic.getArea() + storeDataBasic.getAddress());

        layoutView(viewProtocol3, width, height);
        bitmapProtocol3 = loadBitmapFromView(viewProtocol3);
        fileBase64Str3 = Base64BitmapUtil.bitmapToBase64(bitmapProtocol3, 20);
    }

    private boolean isViewCreated; // 界面是否已创建完成
    private boolean isVisibleToUser; // 是否对用户可见

    /**
     * Fragment 创建期间setUserVisibleHint()方法是在onActivityCreated()之前被调用
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        tryLoadData();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isViewCreated = true;
        tryLoadData();
    }

    public void tryLoadData() {
        if (isViewCreated && isVisibleToUser) {
            if (!TextUtils.isEmpty(SPManager.getInstance().getStoreBasic())) {
                storeDataBasic = mGson.fromJson(SPManager.getInstance().getStoreBasic(), StoreDataBasic.class);
                if (storeDataBasic != null) {
//                    tvSign.setVisibility(View.VISIBLE);
                    llProtocol.setVisibility(View.VISIBLE);
                    if ("0".equals(storeDataBasic.getMerchant_type())) {
                        type = "1";
                        //无需营业执照
//                        llBusiness.setVisibility(View.GONE);
                        //快速入网
                        if (quick_income == 1) {
                            llHandPhoto.setVisibility(View.GONE);
                            //无需电子签名  和 收单协议
//                            tvSign.setVisibility(View.GONE);
                            llProtocol.setVisibility(View.GONE);
                        }
                    } else if ("1".equals(storeDataBasic.getMerchant_type())) {
                        type = "2";
//                        llBusiness.setVisibility(View.VISIBLE);
                    } else if ("3".equals(storeDataBasic.getMerchant_type())) {
                        type = "3";
                        //无需电子签名  和 收单协议
//                        llBusiness.setVisibility(View.VISIBLE);
//                        tvSign.setVisibility(View.GONE);
                        llProtocol.setVisibility(View.GONE);
                    }
                }
            }
            if (!TextUtils.isEmpty(SPManager.getInstance().getStoreCompany())) {
                storeDataCompany = mGson.fromJson(SPManager.getInstance().getStoreCompany(), StoreDataCompany.class);
            }
            if (!TextUtils.isEmpty(SPManager.getInstance().getStoreInfo())) {
                storeDataInfo = mGson.fromJson(SPManager.getInstance().getStoreInfo(), StoreDataInfo.class);
            }

            if ("0".equals(storeDataInfo.getLegal_flag())) {
                llNonLeg.setVisibility(View.VISIBLE);
            } else {
                llNonLeg.setVisibility(View.GONE);
            }

            //0-个人账户, 1-公司账户
            String account_type = storeDataInfo.getAccount_type();

//            if (("2".equals(type)||"3".equals(type))&&"1".equals(account_type)){
//                ll_bank_account_permit_pic.setVisibility(View.VISIBLE);
//                v_bank_account_permit_pic.setVisibility(View.VISIBLE);
//            }else {
//                ll_bank_account_permit_pic.setVisibility(View.GONE);
//                v_bank_account_permit_pic.setVisibility(View.GONE);
//            }

        }
    }


    @OnClick({R.id.ic_prcture_1, R.id.ic_prcture_2, R.id.ic_prcture_3, R.id.ic_prcture_4, R.id.ic_prcture_5, R.id.ic_prcture_6, R.id.ic_prcture_7, R.id.ic_prcture_8, R.id.ic_prcture_9, R.id.ic_prcture_10, R.id.ic_prcture_20, R.id.ic_prcture_21, R.id.ic_prcture_22, R.id.tv_sign, R.id.tv_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ic_prcture_1:

                if ("0".equals(isShow)){
                    Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
                    intent.putExtra("type","0");
                    intent.putExtra("img",imgPath1);
                    startActivity(intent);
                    return;
                }
                imgTpye = 1;
                showPictureSelectorDialog();
                break;
            case R.id.ic_prcture_2:
                if ("0".equals(isShow)){
                    Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
                    intent.putExtra("type","0");
                    intent.putExtra("img",imgPath2);
                    startActivity(intent);
                    return;
                }
                imgTpye = 2;
                showPictureSelectorDialog();
                break;
            case R.id.ic_prcture_3:
                if ("0".equals(isShow)){
                    Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
                    intent.putExtra("type","0");
                    intent.putExtra("img",imgPath3);
                    startActivity(intent);
                    return;
                }
                imgTpye = 3;
                showPictureSelectorDialog();
                break;
            case R.id.ic_prcture_4:
                if ("0".equals(isShow)){
                    Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
                    intent.putExtra("type","0");
                    intent.putExtra("img",imgPath4);
                    startActivity(intent);
                    return;
                }
                imgTpye = 4;
                showPictureSelectorDialog();
                break;
            case R.id.ic_prcture_5:
                if ("0".equals(isShow)){
                    Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
                    intent.putExtra("type","0");
                    intent.putExtra("img",imgPath5);
                    startActivity(intent);
                    return;
                }
                imgTpye = 5;
                showPictureSelectorDialog();
                break;
            case R.id.ic_prcture_6:
                if ("0".equals(isShow)){
                    Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
                    intent.putExtra("type","0");
                    intent.putExtra("img",imgPath6);
                    startActivity(intent);
                    return;
                }
                imgTpye = 6;
                showPictureSelectorDialog();
                break;
            case R.id.ic_prcture_7:
                if ("0".equals(isShow)){
                    Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
                    intent.putExtra("type","0");
                    intent.putExtra("img",imgPath7);
                    startActivity(intent);
                    return;
                }
                imgTpye = 7;
                showPictureSelectorDialog();
                break;
            case R.id.ic_prcture_8:
                if ("0".equals(isShow)){
                    Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
                    intent.putExtra("type","0");
                    intent.putExtra("img",imgPath8);
                    startActivity(intent);
                    return;
                }
                imgTpye = 8;
                showPictureSelectorDialog();
                break;
            case R.id.ic_prcture_9:
                if ("0".equals(isShow)){
                    Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
                    intent.putExtra("type","0");
                    intent.putExtra("img",imgPath9);
                    startActivity(intent);
                    return;
                }
                imgTpye = 9;
                showPictureSelectorDialog();
                break;
            case R.id.ic_prcture_10:
                if ("0".equals(isShow)){
                    Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
                    intent.putExtra("type","0");
                    intent.putExtra("img",imgPath10);
                    startActivity(intent);
                    return;
                }
                imgTpye = 10;
                showPictureSelectorDialog();
                break;
            case R.id.ic_prcture_20:
                if ("0".equals(isShow)){
                    Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
                    intent.putExtra("type","0");
                    intent.putExtra("img",imgPath20);
                    startActivity(intent);
                    return;
                }
                imgTpye = 20;
                showPictureSelectorDialog();
                break;
            case R.id.ic_prcture_21:
                if ("0".equals(isShow)){
                    Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
                    intent.putExtra("type","0");
                    intent.putExtra("img",imgPath21);
                    startActivity(intent);
                    return;
                }
                imgTpye = 21;
                showPictureSelectorDialog();
                break;
            case R.id.ic_prcture_22:
                if ("0".equals(isShow)){
                    Intent intent = new Intent(getContext(), PreviewPicturesActivity.class);
                    intent.putExtra("type","0");
                    intent.putExtra("img",imgPath22);
                    startActivity(intent);
                    return;
                }
                imgTpye = 22;
                showPictureSelectorDialog();
                break;
            case R.id.tv_sign:
                Intent intent = new Intent(mActivity, PenActivity.class);
                intent.putExtra("type", "kdb");
                mActivity.startActivity(intent);
                break;
            case R.id.tv_commit:

                commit();

                break;
        }
    }

    public void commit() {
        if (storeDataBasic == null) {
            ToastUtil.showShort("请填写门店资料基本信息");
            return;
        }
        if (storeDataCompany == null) {
            ToastUtil.showShort("请填写门店资料公司信息");
            return;
        }
        if (storeDataInfo == null) {
            ToastUtil.showShort("请填写门店资料门店信息");
            return;
        }
        if (TextUtils.isEmpty(storeDataBasic.getMerchant_name())) {
            ToastUtil.showShort("请输入门店名称");
            return;
        }
        if (storeDataBasic.getMerchant_name().length()<2) {
            ToastUtil.showShort("门店名称请输入字数大于等于2");
            return;
        }
        if (storeDataBasic.getCrp_profession() == 0) {
            ToastUtil.showShort("请选择法人职业");
            return;
        }


        if (storeDataBasic.getCrp_gender() == 2) {
            ToastUtil.showShort("请选择法人性别");
            return;
        }
        if (TextUtils.isEmpty(storeDataBasic.getMcc_cd())) {
            ToastUtil.showShort("请选择商户行业编号");
            return;
        }
        if (TextUtils.isEmpty(storeDataBasic.getLegal_mobile())) {
            ToastUtil.showShort("请输入法人电话");
            return;
        }
        if (TextUtils.isEmpty(storeDataBasic.getEmail())) {
            ToastUtil.showShort("请输入邮箱");
            return;
        }

        if (TextUtils.isEmpty(storeDataBasic.getLegal_name())) {
            ToastUtil.showShort("请输入联系人姓名");
            return;
        }

        if (TextUtils.isEmpty(storeDataBasic.getContact_mobile())) {
            ToastUtil.showShort("请输入联系人电话");
            return;
        }
        if (storeBean == null && TextUtils.isEmpty(storeDataBasic.getPassword())) {
            ToastUtil.showShort("请输入登录密码");
            return;
        }

        if (TextUtils.isEmpty(storeDataBasic.getLegal_id_card_no())) {
            ToastUtil.showShort("请输入联系人身份证编号");
            return;
        }

        if (TextUtils.isEmpty(storeDataBasic.getLegal_id_card_start())) {
            ToastUtil.showShort("请选择联系人身份证开始时间");
            return;
        }
        if (!storeDataBasic.isLongTime() && TextUtils.isEmpty(storeDataBasic.getLegal_id_card_end())) {
            ToastUtil.showShort("请选择联系人身份证结束时间");
            return;
        }
        if (TextUtils.isEmpty(storeDataBasic.getProvince())) {
            ToastUtil.showShort("请选择省市县区");
            return;
        }
        if (TextUtils.isEmpty(storeDataBasic.getAddress())) {
            ToastUtil.showShort("请输入详细地址");
            return;
        }
        if (TextUtils.isEmpty(storeDataBasic.getLat()) || "null".equals(storeDataBasic.getLat())) {
            ToastUtil.showShort("请选择位置");
            return;
        }

        if ("2".equals(type) || "3".equals(type)) {
            if (TextUtils.isEmpty(storeDataCompany.getLicense_no())) {
                ToastUtil.showShort("请输入营业执照编号");
                return;
            }
            if (TextUtils.isEmpty(storeDataCompany.getLicense_full_name())) {
                ToastUtil.showShort("请输入营业执照名称");
                return;
            }
            if (TextUtils.isEmpty(storeDataCompany.getLicense_address())) {
                ToastUtil.showShort("请输入营业执照地址");
                return;
            }
            if ("3".equals(type) && TextUtils.isEmpty(storeDataCompany.getReg_capital())) {
                ToastUtil.showShort("请输入注册资本");
                return;
            }
            if (TextUtils.isEmpty(storeDataCompany.getLicense_start())) {
                ToastUtil.showShort("请输入营业执照开始时间");
                return;
            }
            if (!storeDataCompany.isLongTime() && TextUtils.isEmpty(storeDataCompany.getLicense_end())) {
                ToastUtil.showShort("请输入营业执照结束时间");
                return;
            }
        }

        if (TextUtils.isEmpty(storeDataInfo.getRate())) {
            ToastUtil.showShort("请输入费率");
            return;
        }
        if (TextUtils.isEmpty(storeDataInfo.getBranch())) {
            ToastUtil.showShort("请选择开户行名称");
            return;
        }
        if (TextUtils.isEmpty(storeDataInfo.getHolder())) {
            ToastUtil.showShort("请输入开户名");
            return;
        }
        if (TextUtils.isEmpty(storeDataInfo.getHolder_mobile())) {
            ToastUtil.showShort("请输入开户预留电话");
            return;
        }
        if (TextUtils.isEmpty(storeDataInfo.getHolder_id_card_no())) {
            ToastUtil.showShort("请输入开户人身份证号");
            return;
        }
        if (TextUtils.isEmpty(storeDataInfo.getBank_card_no())) {
            ToastUtil.showShort("请输入银行卡号");
            return;
        }
        if (TextUtils.isEmpty(imgPath1)) {
            ToastUtil.showShort("请上传门店门头照");
            return;
        }
        if (TextUtils.isEmpty(imgPath2)) {
            ToastUtil.showShort("请上传门店店内照片");
            return;
        }
        if (TextUtils.isEmpty(imgPath3)) {
            ToastUtil.showShort("请上传门店收银台照");
            return;
        }
        if (("2".equals(type) || "3".equals(type)) && TextUtils.isEmpty(imgPath8)) {
            ToastUtil.showShort("请上传营业执照照片");
            return;
        }

        if (TextUtils.isEmpty(imgPath4)) {
            ToastUtil.showShort("请上传银行卡号面照片");
            return;
        }
        if (TextUtils.isEmpty(imgPath9)) {
            ToastUtil.showShort("请上传银行卡反面照片");
            return;
        }


//        if (TextUtils.isEmpty(imgPath10) && "3".equals(type)&& "2".equals(type)) {
//            ToastUtil.showShort("请上传开户许可证照片");
//            return;
//        }


        if (TextUtils.isEmpty(imgPath5)) {
            ToastUtil.showShort("请上传法人身份证正面");
            return;
        }

        if (TextUtils.isEmpty(imgPath6)) {
            ToastUtil.showShort("请上传法人身份证反面");
            return;
        }

        if ("0".equals(storeDataInfo.getLegal_flag())) {
//            if (TextUtils.isEmpty(storeDataInfo.getUnincorporated_name())) {
//                ToastUtil.showShort("请上输入非法人姓名");
//                return;
//            }
//            if (TextUtils.isEmpty(storeDataInfo.getUnincorporated_id())) {
//                ToastUtil.showShort("请上输入非法人省份证号码");
//                return;
//            }
//            if (TextUtils.isEmpty(imgPath20)) {
//                ToastUtil.showShort("请上传非法人授权书");
//                return;
//            }
//            if (TextUtils.isEmpty(imgPath21)) {
//                ToastUtil.showShort("非法人身份证正面");
//                return;
//            }
//            if (TextUtils.isEmpty(imgPath22)) {
//                ToastUtil.showShort("非法人身份证背面");
//                return;
//            }
        }
        if (quick_income == 0 && TextUtils.isEmpty(imgPath7)) {
            ToastUtil.showShort("请上传手持身份证照");
            return;
        }


        storeDataBean.setDoor_pic(imgPath1);
        storeDataBean.setInside_pic(imgPath2);
        storeDataBean.setCashier_desk_pic(imgPath3);
        storeDataBean.setLicense_pic(imgPath8);
        storeDataBean.setBank_card_front_pic(imgPath4);
        storeDataBean.setBank_card_back_pic(imgPath9);
        storeDataBean.setBank_account_permit_pic(imgPath10);
        storeDataBean.setLegal_id_card_front_pic(imgPath5);
        storeDataBean.setLegal_id_card_back_pic(imgPath6);
        storeDataBean.setLegal_id_card_hand_pic(imgPath7);
        storeDataBean.setNon_leg_settle_auth_pic(imgPath20);
        storeDataBean.setNon_leg_idcard_front_pic(imgPath21);
        storeDataBean.setNon_leg_idcard_back_pic(imgPath22);

        showProgress("上传中.....");
        if (("1".equals(type) && quick_income == 0) || "2".equals(type) || "3".equals(type)) {

            saveProtocol1();
            saveProtocol3();

            uploadFileBitmap();
        } else {
            saveProfile();
        }

    }

    /**
     * 保存资料
     */
    public void saveProfile() {
        if (storeDataBasic == null) {
            return;
        }
        if (storeDataCompany == null) {
            return;
        }
        if (storeDataInfo == null) {
            return;
        }

        if (storeBean != null) {
            homePageModel.requestStoreSaveProfile(storeBean.getStore_no(), quick_income, storeDataBasic.getMerchant_type(), storeDataBasic.getMerchant_name(), storeDataBasic.getUser_wx(), storeDataBasic.getContact_name(), storeDataBasic.getLegal_name(),
                    storeDataBasic.getContact_mobile(), storeDataBasic.getLegal_id_card_no(),
                    storeDataBasic.getLegal_id_card_start(), storeDataBasic.getLegal_id_card_end(), storeDataBasic.getProvince(),
                    storeDataBasic.getCity(), storeDataBasic.getArea(), storeDataBasic.getAddress(), storeDataBasic.getLng(),
                    storeDataBasic.getLat(), storeDataCompany.getLicense_no(), storeDataCompany.getLicense_full_name(),
                    storeDataCompany.getLicense_address(), storeDataCompany.getLicense_start(), storeDataCompany.getLicense_end(),
                    storeDataInfo.getShop_type(), storeDataInfo.getSettle_to(), storeDataInfo.getIndustry_id(), storeDataInfo.getRate(),
                    storeDataInfo.getLegal_flag(), storeDataInfo.getUnincorporated_id(), storeDataInfo.getUnincorporated_name(), storeDataInfo.getAccount_type(), storeDataInfo.getBranch(), storeDataInfo.getHolder(),
                    storeDataInfo.getHolder_id_card_no(), storeDataInfo.getHolder_mobile(), storeDataInfo.getBank_card_no(), storeDataBean.getDoor_pic(),
                    storeDataBean.getInside_pic(), storeDataBean.getCashier_desk_pic(), storeDataBean.getLicense_pic(),
                    storeDataBean.getBank_card_front_pic(), storeDataBean.getLegal_id_card_front_pic(), storeDataBean.getLegal_id_card_back_pic(),
                    storeDataBean.getLegal_id_card_hand_pic(), storeDataBean.getNon_leg_settle_auth_pic(), storeDataBean.getNon_leg_idcard_front_pic(),
                    storeDataBean.getNon_leg_idcard_back_pic(), storeDataBean.getTerminal_type(), storeDataBean.getCollect_pic(),
                    storeDataBean.getCollect_pic_two(), storeDataBean.getMerchant_register_pic(), storeDataInfo.getSettlement_cycle(), storeDataCompany.getReg_capital(),
                    storeDataBasic.getKdb_province_id(), storeDataBasic.getKdb_city_id(), storeDataBasic.getKdb_area_id(), storeDataInfo.getBranchCode(), storeDataBasic.getCrp_profession(), storeDataBasic.getCrp_gender(),
                    storeDataBasic.getLegal_mobile(), storeDataBasic.getEmail(), storeDataInfo.getBank(), storeDataBean.getBank_card_back_pic(), storeDataInfo.getUnionpay(), storeDataInfo.getBank_type(), storeDataInfo.getBranch_province(), storeDataInfo.getBranch_city(), storeDataInfo.getOpen_acc_area(),storeDataBean.getBank_account_permit_pic(), String.valueOf(channel_type), storeDataBasic.getMcc_cd(),qr_code_data,new DataCallBack<BaseResponseBean>() {
                        @Override
                        public void onSuccessful(BaseResponseBean bean) {
                            dismissProgress();
                            ToastUtil.showShort(bean.getMsg());
                            mActivity.finish();
                        }

                        @Override
                        public void onFailed(String msg, String code) {
                            dismissProgress();
                        }
                    });
        } else {

            homePageModel.requestStoreIncomeProfile(storeDataBasic.getMerchant_id(), quick_income, storeDataBasic.getMerchant_type(), storeDataBasic.getMerchant_name(), storeDataBasic.getUser_wx(), storeDataBasic.getContact_name(), storeDataBasic.getLegal_name(),
                    storeDataBasic.getContact_mobile(), storeDataBasic.getPassword(), storeDataBasic.getLegal_id_card_no(),
                    storeDataBasic.getLegal_id_card_start(), storeDataBasic.getLegal_id_card_end(), storeDataBasic.getProvince(),
                    storeDataBasic.getCity(), storeDataBasic.getArea(), storeDataBasic.getAddress(), storeDataBasic.getLng(),
                    storeDataBasic.getLat(), storeDataCompany.getLicense_no(), storeDataCompany.getLicense_full_name(),
                    storeDataCompany.getLicense_address(), storeDataCompany.getLicense_start(), storeDataCompany.getLicense_end(),
                    storeDataInfo.getShop_type(), storeDataInfo.getSettle_to(), storeDataInfo.getIndustry_id(), storeDataInfo.getRate(),
                    storeDataInfo.getLegal_flag(), storeDataInfo.getUnincorporated_id(), storeDataInfo.getUnincorporated_name(), storeDataInfo.getAccount_type(), storeDataInfo.getBranch(), storeDataInfo.getHolder(),
                    storeDataInfo.getHolder_id_card_no(), storeDataInfo.getHolder_mobile(), storeDataInfo.getBank_card_no(), storeDataBean.getDoor_pic(),
                    storeDataBean.getInside_pic(), storeDataBean.getCashier_desk_pic(), storeDataBean.getLicense_pic(),
                    storeDataBean.getBank_card_front_pic(), storeDataBean.getLegal_id_card_front_pic(), storeDataBean.getLegal_id_card_back_pic(),
                    storeDataBean.getLegal_id_card_hand_pic(), storeDataBean.getNon_leg_settle_auth_pic(), storeDataBean.getNon_leg_idcard_front_pic(),
                    storeDataBean.getNon_leg_idcard_back_pic(), storeDataBean.getTerminal_type(), storeDataBean.getCollect_pic(),
                    storeDataBean.getCollect_pic_two(), storeDataBean.getMerchant_register_pic(), storeDataInfo.getSettlement_cycle(), storeDataCompany.getReg_capital(),
                    storeDataBasic.getKdb_province_id(), storeDataBasic.getKdb_city_id(), storeDataBasic.getKdb_area_id(), storeDataInfo.getBranchCode(), storeDataBasic.getCrp_profession(), storeDataBasic.getCrp_gender(),
                    storeDataBasic.getLegal_mobile(), storeDataBasic.getEmail(), storeDataInfo.getBank(), storeDataBean.getBank_card_back_pic(), storeDataInfo.getUnionpay(), storeDataInfo.getBank_type(), storeDataInfo.getBranch_province(), storeDataInfo.getBranch_city(), storeDataInfo.getOpen_acc_area(),storeDataBean.getBank_account_permit_pic(),String.valueOf(channel_type), storeDataBasic.getMcc_cd(),qr_code_data, new DataCallBack<BaseResponseBean>() {
                        @Override
                        public void onSuccessful(BaseResponseBean bean) {
                            dismissProgress();
                            ToastUtil.showShort(bean.getMsg());

                            Intent intent = new Intent(getActivity(),SelfRegistrationSuccessfulActivity.class);
                            intent.putExtra("mobile", storeDataBasic.getContact_mobile());
                            startActivity(intent);

                            mActivity.finish();
                        }

                        @Override
                        public void onFailed(String msg, String code) {
                            dismissProgress();
                        }
                    });
        }

    }

    private String TAG = "-------------";

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片、视频、音频选择结果回调
                    List<LocalMedia> mediaList = PictureSelector.obtainMultipleResult(data);
                    if (mediaList.size() > 0) {
                        String imgPath = mediaList.get(0).getCompressPath();
                        requestUploadFile(imgPath);
                    }
                    break;
            }
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.gc();
    }

    public void uploadFileBitmap() {
        homePageModel.requestUploadFile("store", fileBase64Str1, new DataCallBack<ImageBean>() {
            @Override
            public void onSuccessful(ImageBean bean) {
                storeDataBean.setCollect_pic(bean.getData());
                homePageModel.requestUploadFile("store", fileBase64Str3, new DataCallBack<ImageBean>() {
                    @Override
                    public void onSuccessful(ImageBean bean) {
                        storeDataBean.setMerchant_register_pic(bean.getData());
                        saveProfile();
                    }

                    @Override
                    public void onFailed(String msg, String code) {
                        dismissProgress();
                    }
                });
            }

            @Override
            public void onFailed(String msg, String code) {
                dismissProgress();
            }
        });
    }

    /**
     * 上传图片
     */
    public void requestUploadFile(String imgPath) {
        ViewLoading.showProgress(mActivity, "上传中......");
        Bitmap bitmap = BitmapFactory.decodeFile(imgPath);
        String file = Base64BitmapUtil.bitmapToBase64(bitmap);
        homePageModel.requestUploadFile("store", file, new DataCallBack<ImageBean>() {
            @Override
            public void onSuccessful(ImageBean bean) {
                if (isDestroy()) {
                    return;
                }
                Log.e("imgPath=imgPath", imgPath);
                Log.e("imgPath=imgTpye", imgTpye + "");

                if (imgTpye == 1) {
                    imgPath1 = bean.getData();
                    GlideUtils.loadImage(mActivity, icPrcture1, imgPath);
                } else if (imgTpye == 2) {
                    imgPath2 = bean.getData();
                    GlideUtils.loadImage(mActivity, icPrcture2, imgPath);
                } else if (imgTpye == 3) {
                    imgPath3 = bean.getData();
                    GlideUtils.loadImage(mActivity, icPrcture3, imgPath);
                } else if (imgTpye == 4) {
                    imgPath4 = bean.getData();
                    GlideUtils.loadImage(mActivity, icPrcture4, imgPath);
                } else if (imgTpye == 5) {
                    imgPath5 = bean.getData();
                    GlideUtils.loadImage(mActivity, icPrcture5, imgPath);
                } else if (imgTpye == 6) {
                    imgPath6 = bean.getData();
                    GlideUtils.loadImage(mActivity, icPrcture6, imgPath);
                } else if (imgTpye == 7) {
                    imgPath7 = bean.getData();
                    GlideUtils.loadImage(mActivity, icPrcture7, imgPath);
                } else if (imgTpye == 8) {
                    imgPath8 = bean.getData();
                    GlideUtils.loadImage(mActivity, icPrcture8, imgPath);
                } else if (imgTpye == 9) {
                    imgPath9 = bean.getData();
                    GlideUtils.loadImage(mActivity, icPrcture9, imgPath);
                } else if (imgTpye == 10) {
                    imgPath10 = bean.getData();
                    GlideUtils.loadImage(mActivity, icPrcture10, imgPath);
                } else if (imgTpye == 20) {
                    imgPath20 = bean.getData();
                    GlideUtils.loadImage(mActivity, icPrcture20, imgPath);
                } else if (imgTpye == 21) {
                    imgPath21 = bean.getData();
                    GlideUtils.loadImage(mActivity, icPrcture21, imgPath);
                } else if (imgTpye == 22) {
                    imgPath22 = bean.getData();
                    GlideUtils.loadImage(mActivity, icPrcture22, imgPath);
                }

            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    private void showPictureSelectorDialog() {

        selectPicDialog = new BottomSheetDialog(mActivity, R.style.Dialog);
        selectPicDialog.getWindow().getAttributes().windowAnimations = R.style.commomDialog;
        View view = LayoutInflater.from(mActivity).inflate(R.layout.dialog_bottom_select_pictrue, null);
        // 相册
        TextView album = view.findViewById(R.id.tv_select_pictrue_album);
        // 相机
        TextView camera = view.findViewById(R.id.tv_select_pictrue_camera);
        // 取消
        TextView cancel = view.findViewById(R.id.tv_select_pictrue_cancel);

        album.setOnClickListener(v -> handlerPhotoAlbumClick());
        camera.setOnClickListener(v -> handlerCameraClick());
        cancel.setOnClickListener(v -> cancelPictureDialog());

        selectPicDialog.setContentView(view);
        selectPicDialog.show();
    }

    /**
     * 点击相机
     */
    private void handlerCameraClick() {
        openType = 1;
        permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, //
                Manifest.permission.CAMERA};

        if (PermissionsManager.checkPermissions(permissions)) {
            openCamera();
            cancelPictureDialog();
            return;
        }

        permissionsManager.requestPermissionsEach(permissions);
        permissionsManager.setResultListener((allGranted, rejectPermissionList) -> {
            if (allGranted) {
                openCamera();
                cancelPictureDialog();
            } else {
                permissionsManager.showDialog();
            }
        });
    }

    /**
     * 点击相册
     */
    private void handlerPhotoAlbumClick() {
        openType = 0;
        cancelPictureDialog();
        permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, //
                Manifest.permission.READ_EXTERNAL_STORAGE};

        if (PermissionsManager.checkPermissions(permissions)) {
            openPhotoAlbum();
            cancelPictureDialog();
            return;
        }
        permissionsManager.requestPermissionsEach(permissions);
        permissionsManager.setResultListener((allGranted, rejectPermissionList) -> {
            if (allGranted) {
                openPhotoAlbum();
                cancelPictureDialog();
            } else {
                permissionsManager.showDialog();
            }
        });
    }

    /**
     * 打开相机
     */
    private void openCamera() {
        PictureSelector.create(StoreUpDataFragment.this)
                .openCamera(PictureMimeType.ofImage())
                .loadImageEngine(GlideEngine.createGlideEngine())
                .theme(R.style.picture_default_style)//
                .maxSelectNum(1)//
                .minSelectNum(1)//
                .compress(true)//
                .selectionMedia(mediaList)//
                .cropCompressQuality(30)// 裁剪压缩质量 默认90 int
                .minimumCompressSize(100)// 小于100kb的图片不压缩
                .forResult(PictureConfig.CHOOSE_REQUEST);
        cancelPictureDialog();

    }

    /**
     * 打开相册
     */
    private void openPhotoAlbum() {
        PictureSelector.create(StoreUpDataFragment.this)//
                .openGallery(PictureMimeType.ofImage())
                .loadImageEngine(GlideEngine.createGlideEngine())
                .theme(R.style.picture_default_style)//
                .maxSelectNum(1)//
                .minSelectNum(1)//
                .compress(true)//
                .selectionMedia(mediaList)//
                .cropCompressQuality(30)// 裁剪压缩质量 默认90 int
                .minimumCompressSize(100)// 小于20kb的图片不压缩
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    private void cancelPictureDialog() {
        if (selectPicDialog != null) {
            selectPicDialog.cancel();
        }
    }

    @Override
    public void handlerPermissionForActivityResult() {
        if (PermissionsManager.checkPermissions(permissions)) {
            if (openType == 0) {
                openPhotoAlbum();
                cancelPictureDialog();
            } else if (openType == 1) {
                openCamera();
                cancelPictureDialog();
            }
        }
    }


    /**
     * 屏幕截图保存
     */
    private void saveScreenShot(Bitmap bitmap) {
        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
        OutputStream outStream = null;
        String filename;//声明文件名
        //以保存时间为文件名
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        filename = sdf.format(date);
        File file = new File(extStorageDirectory, filename + ".JPEG");//创建文件，第一个参数为路径，第二个参数为文件名
        try {
            outStream = new FileOutputStream(file);//创建输入流
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
            outStream.close();

            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(file);
            intent.setData(uri);
            mActivity.sendBroadcast(intent);

            Toast.makeText(mActivity, "保存成功 打开相册查看", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(mActivity, "exception:" + e,
                    Toast.LENGTH_SHORT).show();

        }
    }

    private Bitmap loadBitmapFromView(View v) {
        int w = v.getWidth();
        int h = v.getHeight();

        Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bmp);

//        c.drawColor(Color.WHITE);
        /** 如果不设置canvas画布为白色，则生成透明 */

        v.layout(0, 0, w, h);
        v.draw(c);

        return bmp;
    }


    private void layoutView(View v, int width, int height) {
        // 指定整个View的大小 参数是左上角 和右下角的坐标
        v.layout(0, 0, width, height);
        int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
        int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.AT_MOST);
        /** 当然，measure完后，并不会实际改变View的尺寸，需要调用View.layout方法去进行布局。
         * 按示例调用layout函数后，View的大小将会变成你想要设置成的大小。
         */
        v.measure(measuredWidth, measuredHeight);
        v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    }

    private Dialog progress;

    public void showProgress(String message) {
        if (progress == null) {
            progress = new Dialog(mActivity, R.style.ShowProgressUtils);
        }
        View view = View.inflate(mActivity, R.layout.dialog_view_loading, null);
        TextView textView = view.findViewById(R.id.loadingText);
        textView.setText(message);
        progress.setContentView(view);
        progress.setCanceledOnTouchOutside(false);
        progress.setCancelable(true);
        try {
            progress.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void dismissProgress() {
        if (progress == null) {
            return;
        }
        try {
            progress.dismiss();
            progress = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}